﻿using ExcelTools.Core.NPOI.Enum;
using ExcelTools.Core.NPOI.Model;
using ExcelTools.Core.NPOI.Extensions;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ExcelTools.Core.NPOI
{
    public class ExportOp
    {
        /// <summary>
        /// 工作表名称
        /// </summary>
        public string SheetName { get; set; }

        /// <summary>
        /// Excel的类型（Xls、Xlsx和WPFExcel）
        /// </summary>
        public ExcelTypeEnum ExcelType { get; set; }

        /// <summary>
        /// Excel的工作簿
        /// </summary>
        private IWorkbook workBook;

        /// <summary>
        /// 工作表对象
        /// </summary>
        private ISheet sheet;

        /// <summary>
        /// 输出的文件路径
        /// </summary>
        public string OutFilePath { get; set; }

        public ExportOp()
        {
        }

        /// <summary>
        /// 初始化必须的参数
        /// </summary>
        /// <param name="outFilePath">输出文件的路径</param>
        /// <param name="sheetName">工作表的名称</param>
        /// <param name="excelType">Excel的版本类型</param>
        public ExportOp(string outFilePath, string sheetName, ExcelTypeEnum excelType)
        {
            this.OutFilePath = outFilePath;
            this.SheetName = sheetName;
            this.ExcelType = excelType;
            CreateWorkbook();
        }

        /// <summary>
        /// 初始化XSSFWorkbook 和 HSSFWorkbook
        /// XSSFWorkbook大于等于Office Excel 2007
        /// HSSFWorkbook Office Excel 2003
        /// </summary>
        private void CreateWorkbook()
        {
            switch (ExcelType)
            {
                case ExcelTypeEnum.Xls:
                    workBook = new HSSFWorkbook();
                    sheet = (HSSFSheet)workBook.CreateSheet(this.SheetName);
                    break;
                case ExcelTypeEnum.Xlsx:
                    workBook = new XSSFWorkbook();
                    sheet = (XSSFSheet)workBook.CreateSheet(this.SheetName);
                    break;
            }
        }

        /// <summary>
        /// 创建数据行
        /// </summary>
        /// <param name="rowcount"></param>
        /// <param name="dic"></param>
        public void CreateRow(int rowcount, Dictionary<int, ExcelColumn> dic)
        {
            IRow row = sheet.CreateRow(rowcount);
            if (dic != null)
            {
                foreach (var item in dic.Keys)
                {
                    ICell currCell = row.CreateCell(item);
                    switch (dic[item].Type)
                    {

                        case ColumnTypeEnum.Int:
                        case ColumnTypeEnum.Long:
                        case ColumnTypeEnum.Decimal:
                        case ColumnTypeEnum.Float:
                        case ColumnTypeEnum.Double:
                            currCell.SetCellValue(dic[item].Value);
                            break;
                        case ColumnTypeEnum.Bool:
                            break;
                        case ColumnTypeEnum.Datetime:
                            currCell.SetCellValue(dic[item].Value.TryConvertDateTime());
                            break;
                        case ColumnTypeEnum.String:
                        default:
                            currCell.SetCellValue(dic[item].Value);
                            break;
                    }
                    //单元格的格式设置
                }
            }
        }

        /// <summary>
        /// 输出文件
        /// </summary>
        public void Write()
        {
            FileStream fs = new FileStream(this.OutFilePath, FileMode.Create);
            workBook.Write(fs);
        }
    }
}
